#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include "strophe.h"

static int reconnect = 0;

int message_handler(xmpp_conn_t *conn, xmpp_stanza_t *stanza, void *userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
    xmpp_stanza_t *body, *reply;
    const char *type;
    char *intext, *replytext;
    int quit = 0;

    body = xmpp_stanza_get_child_by_name(stanza, "body");
    if (body == NULL)
        return 1;
    type = xmpp_stanza_get_type(stanza);
    if (type != NULL && strcmp(type, "error") == 0)
        return 1;

    intext = xmpp_stanza_get_text(body);

    printf("Incoming message from %s: %s\n", xmpp_stanza_get_from(stanza),
           intext);

    reply = xmpp_stanza_reply(stanza);
    if (xmpp_stanza_get_type(reply) == NULL)
        xmpp_stanza_set_type(reply, "chat");

    if (strcmp(intext, "quit") == 0) {
        replytext = strdup("bye!");
        quit = 1;
    } else if (strcmp(intext, "reconnect") == 0) {
        replytext = strdup("alright, let's see what happens!");
        reconnect = 1;
        quit = 1;
    } else {
        replytext = (char *)malloc(strlen(" to you too!") + strlen(intext) + 1);
        strcpy(replytext, intext);
        strcat(replytext, " to you too!");
    }
    xmpp_free(ctx, intext);
    xmpp_message_set_body(reply, replytext);

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);
    free(replytext);

    if (quit)
        xmpp_disconnect(conn);

    return 1;
}


/* define a handler for connection events */
void conn_handler(xmpp_conn_t *conn,
                  xmpp_conn_event_t status,
                  int error,
                  xmpp_stream_error_t *stream_error,
                  void *userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;

    (void)error;
    (void)stream_error;

    if (status == XMPP_CONN_CONNECT) {
        xmpp_stanza_t *pres;
        fprintf(stderr, "DEBUG: connected\n");
        xmpp_handler_add(conn, message_handler, NULL, "message", NULL, ctx);

        /* Send initial <presence/> so that we appear online to contacts */
        pres = xmpp_presence_new(ctx);
        xmpp_send(conn, pres);
        xmpp_stanza_release(pres);
    } else {
        fprintf(stderr, "DEBUG: disconnected\n");
        xmpp_stop(ctx);
    }
}



int main(int argc, const char ** argv) {
	xmpp_initialize();

	long flags = XMPP_CONN_FLAG_DISABLE_TLS;

    /* pass NULL instead to silence output */
    auto log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    /* create a context */
    auto ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    auto conn = xmpp_conn_new(ctx);


    /* configure connection properties (optional) */
    xmpp_conn_set_flags(conn, flags);


	xmpp_conn_set_jid(conn, "ada@tf-20220713vmsl");
	xmpp_conn_set_pass(conn, "123");

	// if(xmpp_connect_client(conn, "172.25.78.56", 0, conn_handler, ctx) == XMPP_EOK) {
	// 	printf("connect success\n");
        xmpp_run_once(ctx, 3000);
	// }

    /* release our connection */
    xmpp_conn_release(conn);

    /* release our context */
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

	return 0;
}
